<!--  -->
<template>
  <div class="comment-container">
    <el-card>
      <div slot="header">
        <!-- 面包屑导航 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>留言管理</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <el-table
        :data="comment"
        style="width: 100%"
        class="table-list"
        stripe
      >
        <el-table-column
          prop="name"
          width="200"
          label="昵称"
        >
        </el-table-column>
        <el-table-column
          prop="content"
          label="留言内容"
        >
        </el-table-column>
        <el-table-column
          prop="email"
          width="200"
          label="联系方式"
        >
        </el-table-column>
        <el-table-column
          prop="created_at"
          width="200"
          label="时间"
        >
          <!-- <template slot-scope="scope">
            {{scope.row.comment_status?'正常':'关闭'}}
          </template> -->
        </el-table-column>
        <el-table-column
          prop="date"
          width="100"
          label="状态"
        >
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.state"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="onStatusChange(scope.row)"
              :disabled="scope.row.statusDisabled"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          width="100"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="danger"
              circle
              icon="el-icon-delete"
              @click="onDeleteComment(scope.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size.sync="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        background
        :total="totalCount"
      />
    </el-card>
  </div>
</template>

<script>
import { changeStatus, getCommet, deleteComment } from '@/api/comment'
export default {
  name: 'CommentIndex',
  components: {},
  props: {},
  data () {
    return {
      comment: [],
      totalCount: 0,
      pageSize: 10,
      page: 1
    }
  },
  computed: {},
  watch: {},
  created () {
    this.loadComment()
  },
  mounted () { },
  methods: {
    handleSizeChange (val) {
      this.loadComment(1)
    },
    handleCurrentChange (val) {
      this.loadComment(val)
    },
    async loadComment (page = 1) {
      // 让分页组件激活的页码和请求数据的页码操持一致
      this.page = page
      const res = await getCommet(
        {
          page,
          per_page: this.pageSize
        }
      )
      const results = res.data.data
      results.forEach(article => {
        article.statusDisabled = false
      })
      this.comment = results
      this.totalCount = res.data.total_count
    },
    onDeleteComment (id) {
      // console.log(id)
      this.$confirm('是否删除该评论', '删除提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await deleteComment(id.toString())
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.loadComment(this.page)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
      })
    },
    async onStatusChange (article) {
      // 禁用开关
      article.statusDisabled = true
      const opt = {
        id: article.id,
        state: article.state
      }
      await changeStatus(opt)
      // article.state = !article.state
      article.statusDisabled = false
      // this.article.state = !article.state
      this.$message({
        type: 'success',
        message: article.state ? '留言审核通过' : '留言审核失败'
      })
    }
  }
}
</script>

<style lang='less' scoped>
.table-list {
  margin-bottom: 30px;
}
</style>
